DICTIONARIES: CREATION, ACCESSING VALUES, AND DICTIONARY METHODS.

Dictionaries in Python are data structures that store key-value pairs. They allow you to access values quickly based on their corresponding keys. Here's how you can create dictionaries, access their values, and use some common dictionary methods:

  1. Creating a Dictionary: You can create a dictionary by enclosing key-value pairs in curly braces {}, separated by colons :. Each key in the dictionary must be unique.
python
# Empty dictionary empty_dict = {} # Dictionary with key-value pairs my_dict = { "name": "John", "age": 30, "city": "New York" }
  1. Accessing Values: You can access the values of a dictionary using the corresponding keys in square brackets [].
python
# Accessing values name_value = my_dict["name"] age_value = my_dict["age"] city_value = my_dict["city"] print(name_value) # Output: John print(age_value) # Output: 30 print(city_value) # Output: New York
  1. Dictionary Methods:

    a. dict.get(key, default=None): This method returns the value for the given key if it exists in the dictionary, otherwise, it returns the default value. If no default value is provided, it returns None.

    python
    # Using get() country = my_dict.get("country", "Unknown") print(country) # Output: Unknown (since "country" key is not present)

    b. dict.keys(): This method returns a view object that displays a list of all the keys in the dictionary.

    python
    # Using keys() keys_list = my_dict.keys() print(keys_list) # Output: dict_keys(['name', 'age', 'city'])

    c. dict.values(): This method returns a view object that displays a list of all the values in the dictionary.

    python
    # Using values() values_list = my_dict.values() print(values_list) # Output: dict_values(['John', 30, 'New York'])

    d. dict.items(): This method returns a view object that displays a list of key-value tuples.

    python
    # Using items() items_list = my_dict.items() print(items_list) # Output: dict_items([('name', 'John'), ('age', 30), ('city', 'New York')])

    e. dict.update(other_dict): This method updates the dictionary with the key-value pairs from another dictionary.

    python
    # Using update() additional_info = {"occupation": "Engineer", "hobbies": ["Reading", "Cooking"]} my_dict.update(additional_info) print(my_dict) # Output: {'name': 'John', 'age': 30, 'city': 'New York', 'occupation': 'Engineer', 'hobbies': ['Reading', 'Cooking']}

    f. dict.pop(key, default=None): This method removes the key-value pair for the given key and returns the corresponding value. If the key is not found, it returns the default value (if provided), otherwise raises a KeyError.

    python
    # Using pop() age_value = my_dict.pop("age") print(age_value) # Output: 30

    g. dict.clear(): This method removes all the key-value pairs from the dictionary, making it empty.

    python
    # Using clear() my_dict.clear() print(my_dict) # Output: {}

These are some of the essential operations you can perform with dictionaries in Python. Dictionaries are versatile data structures and are widely used for various purposes due to their efficiency in key-based data retrieval.